package com.telecom.weixi.parse;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.telecom.weixi.util.Utils;

public class MenuInfoParse extends DefaultHandler {

	List<MenuInfo> infoList;
	MenuInfo infoItem;
	StringBuffer buffer;
	
	private static final String Tag_Head = "MenuBean";
	private static final String Tag_Layer_Id = "layer_id";
	private static final String Tag_Menu_Id = "menu_id";
	private static final String Tag_Menu_Name = "menu_name";
	private static final String Tag_Menu_Order = "menu_order";
	private static final String Tag_PMenu_Id = "parent_menu_id";
	
	public List<MenuInfo> getData(){
		return infoList;
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		buffer.append(ch, start, length);
		super.characters(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
		if(localName.equalsIgnoreCase(Tag_Head)){
			infoList.add(infoItem);
			Utils.debug("test", infoList.size() + ": " + infoItem.toString());
		}else if(localName.equalsIgnoreCase(Tag_Layer_Id)){
			infoItem.setLayerId(Integer.valueOf(buffer.toString().trim()));
			buffer.setLength(0);
		}else if(localName.equalsIgnoreCase(Tag_Menu_Id)){
			infoItem.setMenuId(buffer.toString().trim());
			buffer.setLength(0);
		}else if(localName.equalsIgnoreCase(Tag_Menu_Name)){
			infoItem.setMenuName(buffer.toString().trim());
			buffer.setLength(0);
		}else if(localName.equalsIgnoreCase(Tag_Menu_Order)){
			infoItem.setMenuOrder(Integer.valueOf(buffer.toString().trim()));
			buffer.setLength(0);
		}else if(localName.equalsIgnoreCase(Tag_PMenu_Id)){
			infoItem.setpMenuId(buffer.toString().trim());
			buffer.setLength(0);
		}
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
		if(infoList == null){
			infoList = new ArrayList<MenuInfo>();
		}
		buffer = new StringBuffer();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName, attributes);
		if(localName.equalsIgnoreCase(Tag_Head)){
			infoItem = new MenuInfo();
		}
	}
	
}
